package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbGrant;
import com.ruoyi.system.service.IStbCompanyService;
import com.ruoyi.system.service.IStbGrantService;

/**
 * 资助Controller
 * 
 * @author ruoyi
 * @date 2020-02-08
 */
@Controller
@RequestMapping("/system/grant")
public class StbGrantController extends BaseController
{
    private String prefix = "system/grant";

    @Autowired
    private IStbGrantService stbGrantService;
    @Autowired
	private IStbCompanyService stbCompanyService;




    @RequiresPermissions("system:grant:view")
    @GetMapping()
    public String grant(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/grant";
    }

	/*
	 * 查询资助  time:时间  num:资助数量
	 */
    @PostMapping("/moneycount")
    @ResponseBody
    public List<Map<String,Object>> selectGrantMoneyCount(){
        StbGrant stbGrant = new StbGrant();
        List<Map<String, Object>> grantMoneyList = stbGrantService.selectGrantMoneyCount(stbGrant);
        return grantMoneyList;
    }
    
    /**
     * 查询资助列表
     */
    @RequiresPermissions("system:grant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StbGrant stbGrant)
    {
        startPage();
        List<StbGrant> list = stbGrantService.selectStbGrantList(stbGrant);
        return getDataTable(list);
    }

    /**
     * 导出资助列表
     */
    @RequiresPermissions("system:grant:export")
    @Log(title = "资助", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StbGrant stbGrant)
    {
        List<StbGrant> list = stbGrantService.selectStbGrantList(stbGrant);
        ExcelUtil<StbGrant> util = new ExcelUtil<StbGrant>(StbGrant.class);
        return util.exportExcel(list, "grant");
    }

    /**
     * 新增资助
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/add";
    }

    /**
     * 新增保存资助
     */
    @RequiresPermissions("system:grant:add")
    @Log(title = "资助", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StbGrant stbGrant)
    {
    	stbGrant.setCreateBy(ShiroUtils.getLoginName());
    	stbGrant.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbGrantService.insertStbGrant(stbGrant));
    }

    /**
     * 修改资助
     */
    @GetMapping("/edit/{grantId}")
    public String edit(@PathVariable("grantId") Long grantId, ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        StbGrant stbGrant = stbGrantService.selectStbGrantById(grantId);
        mmap.put("stbGrant", stbGrant);
        return prefix + "/edit";
    }

    /**
     * 修改保存资助
     */
    @RequiresPermissions("system:grant:edit")
    @Log(title = "资助", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StbGrant stbGrant)
    {
    	stbGrant.setCreateBy(ShiroUtils.getLoginName());
    	stbGrant.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbGrantService.updateStbGrant(stbGrant));
    }

    /**
     * 删除资助
     */
    @RequiresPermissions("system:grant:remove")
    @Log(title = "资助", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stbGrantService.deleteStbGrantByIds(ids));
    }
}
